package TraitementMessages;

import mainProcess.MainProcessing;
import com.lo23.data.objinterface.*;
import com.lo23.data.objpicture.*;
import com.lo23.data.serializable.*;
import java.util.*;
import java.util.logging.Logger;
import java.util.logging.Level;


public class SearchPictureByKeyWordsMessage extends PictureMessage {

    private List<String> keywords;
    static final long serialVersionUID = 23201020L;
	public static int maxWidth = 30;
	public static int maxHeight = 40;

    public SearchPictureByKeyWordsMessage (String senderUID, List<String> keywords) {
        super(senderUID);
        this.keywords = keywords;
    }
    
    public void handleReceiption(MainProcessing mainProcessing)
    {
        /*
         * Récupération de toute la liste des photos de l'utilisateur visibles
         * pour l'envoyeur.
         */
        List<Picture> visibleImages;
        visibleImages = mainProcessing.getTraitementData().getVisibleImages(this.senderUID);

        // Tri des images selon si elles correspondent aux mots-clés
        HashSet<Picture> searchedImages = new HashSet<Picture>();
        for (Picture image : visibleImages)
            for (String keyword : keywords)
                if (image.hasKeyword(keyword)){
                    searchedImages.add(image);
                    break;
                }
        
        // Création d'un message SearchPictureResultMessage avec les vignettes
        LocalUser lUser = mainProcessing.getDataInterface().getLocalUser(null);
        ArrayList<PictureThumbnail> thumbnails = new ArrayList<PictureThumbnail>();
        for (Picture image : searchedImages)
            thumbnails.add(image.getPictureThumbnail(maxWidth, maxHeight, lUser.getUid()));
        SearchPictureResultMessage newMessage = new SearchPictureResultMessage(lUser.getuID(), thumbnails);

        // Envoi du message
        ArrayList<String> destUIDs = new ArrayList<String>();
        destUIDs.add(this.senderUID);
        try {
            mainProcessing.getNetworkManager().sendMessage(destUIDs, newMessage);
        } catch (Exception ex) {
            Logger.getLogger(SearchPictureAllMessage.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
